package com.globallogic.training.javase.university.curriculum;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.globallogic.training.javase.university.member.Group;

public class Stream {
	private String id;

	private String name;
	
	private Curriculum curriculum;
	
	private Set<Group> groups;
	
	public Stream() {
		groups = new HashSet<Group>();
	}

	public Stream(String id, String name) {
		if (StringUtils.isBlank(id)) {
			throw new IllegalArgumentException(
					"Input 'id' argument should not be null");
		}

		if (StringUtils.isBlank(name)) {
			throw new IllegalArgumentException(
					"Input 'name' argument should not be null.");
		}

		this.id = id;
		this.name = name;
		groups = new HashSet<Group>();
	}

	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Curriculum getCurriculum() {
		return curriculum;
	}
	
	public void setCurriculum(Curriculum curriculum) {
		this.curriculum = curriculum; 
	}
	
	public boolean addGroup(Group group) {
		return groups.add(group);
	}
	
	public boolean removeGroup(Group group) {
		return groups.remove(group);
	}
	
	public Group getGroup(String id)  {
		for (Group group : groups) {
			if (group.getId().equals(id)) {
				return group;
			}
		}
		
		return null;
	}
	
	public Group[] listGroups()  {
		return groups.toArray(new Group[groups.size()]);
	}

	@Override
	public int hashCode() {
		return 1 * 31 + id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!getClass().equals(obj.getClass())) {
			return false;
		}

		Stream other = (Stream) obj;
		return id.equals(other.id);
	}

	@Override
	public String toString() {
		return id + " " + name;
	}
}
